//
//  SIXMainViewController.m
//  FastDoctor_6
//
//  Created by cool on 16/2/29.
//  Copyright © 2016年 cool. All rights reserved.
//

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height

#import "SIXMainViewController.h"
#import "SIXMainNavController.h"
#import "UIViewController+MMDrawerController.h"
#import "SIXWeatherView.h"
#import "SIXScrollView.h"
#import "SIXProvinceViewController.h"
#import "SIXCitiesViewController.h"
#import "SIXClassModel.h"
#import "SIXClassViewCell.h"


@interface SIXMainViewController ()<SIXWeatherViewDelegate,UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate >

@property (nonatomic, strong) SIXWeatherView *weatherView;

@property (nonatomic, strong) SIXScrollView *scrollView;

@property (nonatomic, strong) UILabel *famousDocLabel;

@property (nonatomic, strong) UICollectionView *classView;

@property (nonatomic, strong) SIXProvinceViewController *provinceVC;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@end

//定义重用标示符
static NSString *identifier = @"collectionCell";


@implementation SIXMainViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];
    [self setNavBarItem];
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    [self setupUI];
    
    
    
    self.weatherView.delegate = self;
}

- (void)placeBtnClickWith:(SIXWeatherView *)weatherView{
    SIXProvinceViewController *provinceVC = [[SIXProvinceViewController alloc] init];
    self.provinceVC = provinceVC;
    [self.navigationController pushViewController:provinceVC animated:YES];
    
}

- (NSString *)title{
    return @"快医";
}
    


- (void)setNavBarItem{
    UIButton *btn = [[UIButton alloc] init];
    [btn setBackgroundImage:[UIImage imageNamed:@"homeleft"] forState:UIControlStateNormal];
    
    [btn sizeToFit];
    
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)btnAction{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];

}


- (void)setupUI
{
    [self addWeatherView];
    [self addScrollView];
    [self addfamousDoctor];
    [self setFlowLayout];
    
}

#pragma mark -
#pragma mark - 添加天气页面
- (void)addWeatherView
{
    if (_weatherView == nil) {
       //自适应屏幕高度设置
        CGFloat H1 = ScreenH / 667;
        
        CGFloat H = 65 * H1;
        
        SIXWeatherView *weatherView = [[SIXWeatherView alloc] init];
       
        weatherView.frame = CGRectMake(0, 0, ScreenW, H);
        
        [weatherView layoutSubviews];
    
        self.weatherView = weatherView;
        
    }
    
    [self.view addSubview:_weatherView];
}


#pragma mark -
#pragma mark - 添加scrollview页面
- (void)addScrollView{
    if (_scrollView == nil) {
       //自适应屏幕高度设置
        CGFloat H1 = ScreenH / 667;
        
        CGFloat H = 235 * H1;
        
        CGFloat Y = CGRectGetMaxY(self.weatherView.frame);
        
        SIXScrollView *scrollView = [[SIXScrollView alloc] init];
        
        scrollView.frame = CGRectMake(0, Y, ScreenW, H);
    
        scrollView.contentSize = CGSizeMake(ScreenW * 3, H);
        
        scrollView.pagingEnabled = YES;
        
        scrollView.delegate = self;
        
        scrollView.showsHorizontalScrollIndicator = NO;
        
        self.scrollView = scrollView;
        
        CGFloat pageControlH = CGRectGetMaxY(self.scrollView.frame) - 20;
        scrollView.pageControl.center = CGPointMake(ScreenW / 2,pageControlH);
        
        [self.view addSubview:scrollView];
        
        self.scrollView.pageControl.currentPage = scrollView.pageCount;
        
        [self.view addSubview:scrollView.pageControl];
    
    }
}

#pragma mark -
#pragma mark - 添加名医通
- (void)addfamousDoctor{
   //自适应屏幕高度设置
    
    CGFloat H1 = ScreenH / 667;
    
    CGFloat H = 40 / H1;
    
    CGFloat Y = CGRectGetMaxY(self.scrollView.frame);
    
    //名医通button
   // UIButton *famousDocButton = [[UIButton alloc] initWithFrame:CGRectMake(0, Y+0.5, ScreenW, H)];
    UILabel *famousDocLabel = [[UILabel alloc] init];

    self.famousDocLabel = famousDocLabel;
    
    famousDocLabel.frame = CGRectMake(0, Y, ScreenW, H);
    
    famousDocLabel.text = @"      名医通";
    
    famousDocLabel.font = [UIFont systemFontOfSize:13];
    
    famousDocLabel.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:famousDocLabel];
    
    //蓝色图片
    //rCGFloat tagImageY = CGRectGetMinY(self.famousDocLabel.frame) ;
    
    UIImageView *tagImage = [[UIImageView alloc] init];

    tagImage.image = [UIImage imageNamed:@"yj"];
    
    tagImage.frame = CGRectMake(10, Y+8*H1, 10, H);
    
    [tagImage sizeToFit];
    
    
    [self.view addSubview:tagImage];
    
    //两根线
    
    UIView *topline = [[UIView alloc] initWithFrame:CGRectMake(0,Y,ScreenW,0.5)];
    topline.backgroundColor = [UIColor grayColor];
    [self.view addSubview:topline];
    
    CGFloat bottomLineY = CGRectGetMaxY(self.famousDocLabel.frame)- 0.5;
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0,bottomLineY-0.5,ScreenW,0.5)];
    bottomLine.backgroundColor = [UIColor grayColor];
    [self.view addSubview:bottomLine];
    
}

#pragma mark -
#pragma mark - 添加疾病分类按钮
- (void)setFlowLayout{
    
    CGFloat Y = CGRectGetMaxY(self.famousDocLabel.frame);
    
    CGFloat H = self.view.frame.size.height  - Y-64;
    
    CGFloat itemW = ScreenW / 3;
    
    CGFloat itemH = H / 2;
    
    
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    _flowLayout.itemSize = CGSizeMake(itemW,itemH);
    
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _flowLayout.minimumInteritemSpacing = 0;
    
    UICollectionView *classView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, Y, ScreenW, H) collectionViewLayout:_flowLayout];
    
    self.classView = classView;
    
    [self.classView registerClass:[SIXClassViewCell class] forCellWithReuseIdentifier:identifier];
    
    self.classView.dataSource = self;
    
    classView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:classView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SIXClassViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    SIXClassModel *classModel = self.dataArray[indexPath.row];
    
    cell.classModel = classModel;
    
    return cell;
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
}

#pragma mark -
#pragma mark - 懒加载
- (NSArray *)dataArray{
    if (nil == _dataArray) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Class.plist" ofType:nil];
        
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *mute = [NSMutableArray array];
        
        for (NSDictionary *dict in tempArray) {
            SIXClassModel *classModel = [SIXClassModel ClassModelWithDict:dict];
            [mute addObject:classModel];
        }
        
        _dataArray = mute;
    }
    return _dataArray;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
